package com.wlyy.his.controller.user.pc;

import cn.hutool.core.bean.BeanUtil;
import com.wlyy.his.bean.user.pc.dto.PatientDto;
import com.wlyy.his.bean.user.pc.vo.PatientListVo;
import com.wlyy.his.bean.user.pc.vo.PatientVo;
import com.wlyy.his.common.util.desensitization.DesensitizationUtils;
import com.wlyy.his.entity.pc.PatientCertificate;
import com.wlyy.his.service.pc.IPatientCertificateService;
import com.wlyy.his.service.pc.IPatientService;
import com.wlyy.his.entity.pc.Patient;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlyy.his.common.api.CommonResult;
import com.wlyy.his.service.pc.impl.PatientCertificateServiceImpl;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  patient前端控制器  RestController注解 将结果以JSON形式返回
 * </p>
 *
 * @author zh
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/patient")
public class PatientController {
    @Resource
    public IPatientService patientService;
    @Resource
    private IPatientCertificateService patientCertificateService;

    /**
     * 保存修改公用 POST请求方式
     * @param patientDto 保存的对象
     * @return CommonResult
     */
    @PostMapping("/save")
    public CommonResult save(@RequestBody PatientDto patientDto) {
        boolean insert = patientService.insert(patientDto);
        if (insert){
            return CommonResult.success(null);
        }else {
            return CommonResult.failed();
        }
    }


    /**
     * 查询一个  支持 GET
     *
     * @param patientId 查找对象的主键ID
     * @return JsonReturn
     */
    @GetMapping("findOne/{patientId}")
    public CommonResult findOne(@PathVariable String patientId) {
        PatientVo patientVo = patientService.findByPatientId(patientId);
        return CommonResult.success(patientVo);
    }

    /**查询所有 支持GET
     * @return CommonResult
     */
    @GetMapping("findAll")
    public CommonResult findAll() {
        List<PatientListVo> patientListVos = patientService.listByAccountId();
        if (BeanUtil.isNotEmpty(patientListVos)){
            patientListVos.forEach(patientListVo -> {
                patientListVo.setName(DesensitizationUtils.desensitizedName(patientListVo.getName()));
            });
        }
        return CommonResult.success(patientListVos);
    }



    /**
    * 保存修改公用 POST请求方式
    * @param patientDto 根据id修改对象
    * @return JsonReturn
    */
    @PostMapping("/update")
    public CommonResult update(PatientDto patientDto) {
        boolean update = patientService.updatePatient(patientDto);
        if (update){
            return CommonResult.success(null);
        }else {
            return CommonResult.failed();
        }
    }




}

